package etc.com.market;

import java.util.ArrayList;

import Controls.JsonDataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;
import etc.com.sql.DBAdapter;

public class moreImage extends Activity {
	ArrayList<String> text = new ArrayList<String>();
	int n;
	Bitmap[] imageIDs;
	private static final int REQUEST_CODE = 10;
	String storename;
	String productId;
	String str;
	String previous;
	Cursor cursor;

	// R.drawable.mandarin_orange, R.drawable.oranges, R.drawable.pineap,
	// R.drawable.pomegranate, R.drawable.sweetorange, R.drawable.tangelo

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.moreimage);

		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}

		String messageReceive = extras.getString("message");
		productId = extras.getString("productId");
		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();

			System.out.println("MESSAGE: " + str);
			previous = str.substring(0, str.indexOf(";"));

			str = str.substring(str.indexOf(";") + 1);

			storename = str.substring(0, str.indexOf(";"));

			str = str.substring(str.indexOf(";") + 1);

			TextView view = (TextView) findViewById(R.id.storename);
			view.setText(storename);

//			TextView view1 = (TextView) findViewById(R.id.headerprevious);
//			view1.setText(previous);
		}
		
		DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
		System.out.println("PRODUCTID: " + productId);
		cursor = dbAdapter.getProductDetailsById(productId);
		cursor.moveToNext();
		
		String image = cursor.getString(8);
		System.out.println("IMAGE FROM DB: " + image);
		image = image.substring(1, image.length() - 1);
		String[] images = image.split(",");
		imageIDs = new Bitmap[images.length];
		for(int i = 0; i < images.length; i++){
			String imagePath = images[i].trim().substring(1, images[i].trim().length() - 1);
			System.out.println("IMAGE PATH: " + imagePath);
			String imageValue = JsonDataHelper.getImageContent(imagePath);
			byte[] decodeBytes = Base64.decode(imageValue.getBytes(), Base64.DEFAULT);
			imageIDs[i] = BitmapFactory.decodeByteArray(decodeBytes, 0, decodeBytes.length);
		}
		
		GridView gridView = (GridView) findViewById(R.id.gridview);
		gridView.setAdapter(new ImageAdapter(this));

		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// onClickGrid(
				// v,
				// ("List Products;" + text.get(position).toString() + ";"
				// + "More picture for product" + ";" + imageIDs[position]));
				onClickGrid(v, imageIDs, position);
			}
		});

	}

	public void onClickGrid(View view, Bitmap[] message, int position) {
		cursor.close();
		Intent i = new Intent(this, GalleryAct.class);
//		i.putExtra("message", message);
		i.putExtra("productId", productId);
		i.putExtra("selected", position);
		i.putExtra("arraySize", imageIDs.length);
		i.putExtra("storeName", storename);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	// function use for footer button ==============================
	public void Previous(View view) {
		cursor.close();
//		onClickPre(view, ("Product Detail;" + storename));
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void Home(View view) {
		cursor.close();
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, ("店舗詳細;" + storename));
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, ("店舗詳細;" + storename));
	}

	public void Info(View view) {
		onClickInfo(view, storename);
	}
	
	public void Contact(View view) {
		onClickContact(view, storename);
	}

	// ===================================================================
	public void onClickPre(View view, String message) {
		cursor.close();
		Toast.makeText(this, "You clicked on go to product detail button", 1)
				.show();

		Intent i = new Intent(this, ProductDetail.class);

		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSpec(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSearch(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, SearchOursProducts.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickInfo(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		cursor.close();
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public static class ViewHolder  
    {  
        public ImageView imgViewFlag;  
    }  
	
	public class ImageAdapter extends BaseAdapter {
//		private Context context;
		private Activity activity;
		public ImageAdapter(Activity activity) {
			this.activity = activity;
		}

		// ---returns the number of images---
		public int getCount() {
			return imageIDs.length;
		}

		// ---returns the ID of an item---
		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		// ---returns an ImageView view---
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder view; 
			LayoutInflater inflator = activity.getLayoutInflater(); 
			if (convertView == null) {
				view = new ViewHolder();
				convertView = inflator.inflate(R.layout.gridview_row, null);
				view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
				convertView.setTag(view);  
			} else {
				view = (ViewHolder) convertView.getTag();
				
			}
			Bitmap kinhlup = BitmapFactory.decodeResource(getResources(), R.drawable.icon_showing_on_image_s1);
			Bitmap mainImage = imageIDs[position];
			
			view.imgViewFlag.setImageBitmap(overlay(mainImage, kinhlup));
			
//			view.imgViewFlag.setImageBitmap(imageIDs[position]);
			view.imgViewFlag.setScaleType(ScaleType.FIT_XY);
			return convertView;
		}
		
		public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
	        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
	        Canvas canvas = new Canvas(bmOverlay);
	        canvas.drawBitmap(bmp1, new Matrix(), null);
	        canvas.drawBitmap(bmp2, bmp1.getWidth() - bmp2.getWidth() , bmp1.getHeight() - bmp2.getHeight(), null);
	        return bmOverlay;
	    }

	}
}
